<?php
require_once '../includes/functions.php';
check_login();

$error = '';
$success = '';
$selected_template = $_GET['template'] ?? '';

// 获取模板列表
$templates_dir = __DIR__ . '/../templates';
$templates = array_filter(scandir($templates_dir), function($item) use ($templates_dir) {
    return $item !== '.' && $item !== '..' && is_dir($templates_dir . '/' . $item);
});

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $subdomain = $_POST['subdomain'] ?? '';
    $template = $_POST['template'] ?? '';
    
    // 验证输入
    if (empty($subdomain) || empty($template)) {
        $error = '请填写所有必填字段';
    } elseif (!preg_match('/^[a-zA-Z0-9-]+$/', $subdomain)) {
        $error = '二级域名只能包含字母、数字和连字符';
    } elseif (!in_array($template, $templates)) {
        $error = '无效的模板';
    } else {
        $landing_page_path = get_landing_page_path($subdomain);
        
        // 检查二级域名是否已存在
        if (file_exists($landing_page_path)) {
            $error = '此二级域名已被使用';
        } else {
            // 创建落地页
            if (create_landing_page($subdomain, $template)) {
                $success = '落地页创建成功';
                // 延迟跳转到编辑页面
                header('Refresh: 2; URL=edit_subdomain.php?subdomain=' . urlencode($subdomain));
            } else {
                $error = '创建落地页时发生错误';
            }
        }
    }
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>创建落地页 - 广告落地页管理系统</title>
    <link rel="stylesheet" href="css/admin.css">
</head>
<body>
    <div class="admin-container">
        <div class="sidebar">
            <div class="sidebar-header">
                <h2>落地页管理系统</h2>
            </div>
            <ul class="sidebar-menu">
                <li><a href="index.php">仪表盘</a></li>
                <li><a href="create_subdomain.php" class="active">创建落地页</a></li>
                <li><a href="logout.php">退出登录</a></li>
            </ul>
        </div>
        
        <div class="main-content">
            <div class="header">
                <h1>创建落地页</h1>
                <div class="user-info">
                    欢迎, <?php echo htmlspecialchars($_SESSION['username']); ?>
                </div>
            </div>
            
            <div class="content">
                <div class="card">
                    <div class="card-header">
                        <h2 class="card-title">创建新的落地页</h2>
                    </div>
                    
                    <?php if ($error): ?>
                        <div class="error-message"><?php echo htmlspecialchars($error); ?></div>
                    <?php endif; ?>
                    
                    <?php if ($success): ?>
                        <div class="success-message"><?php echo htmlspecialchars($success); ?></div>
                    <?php endif; ?>
                    
                    <form method="POST" action="">
                        <div class="form-group">
                            <label for="template">选择模板</label>
                            <select name="template" id="template" class="form-control" required>
                                <option value="">请选择模板</option>
                                <?php foreach ($templates as $template): ?>
                                    <option value="<?php echo htmlspecialchars($template); ?>"
                                            <?php echo $template === $selected_template ? 'selected' : ''; ?>>
                                        <?php echo htmlspecialchars($template); ?>
                                    </option>
                                <?php endforeach; ?>
                            </select>
                        </div>
                        
                        <div class="form-group">
                            <label for="subdomain">二级域名</label>
                            <div class="input-group">
                                <input type="text" id="subdomain" name="subdomain" class="form-control" 
                                       placeholder="输入二级域名" required pattern="[a-zA-Z0-9-]+"
                                       title="只能使用字母、数字和连字符">
                                <span class="input-group-text">.jl25.net</span>
                            </div>
                            <small class="form-text">二级域名只能包含字母、数字和连字符</small>
                        </div>
                        
                        <div class="form-actions">
                            <button type="submit" class="btn btn-success">
                                <svg viewBox="0 0 24 24" width="16" height="16">
                                    <path fill="currentColor" d="M19 13h-6v6h-2v-6H5v-2h6V5h2v6h6v2z"/>
                                </svg>
                                创建落地页
                            </button>
                            <a href="index.php" class="btn">
                                <svg viewBox="0 0 24 24" width="16" height="16">
                                    <path fill="currentColor" d="M20 11H7.83l5.59-5.59L12 4l-8 8 8 8 1.41-1.41L7.83 13H20v-2z"/>
                                </svg>
                                返回
                            </a>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
